var imgNo = "<img width='15px' src='images/n.png'/>";
var imgYes = "<img width='15px' src='images/y.png'>";

function validateTip(element,css,tipString,status){
    element.css(css);
    element.html(tipString);
    element.attr("validateStatus",status);
}

$(function () {

    var up = $("#up");
    var down = $("#down");
    var first = $("#first");
    var second = $("#second");
    var third = $("#third");
    var last = $("#last");
    var currentNo = $("#pageNo");
    var pageNo = 1;

    var p1 = first.attr("name");
    var p2 = second.attr("name");
    var p3 = third.attr("name");
    var form = $("#form");

    var lv1 = $("#lv1");
    var lv2 = $("#lv2");
    var lv3 = $("#lv3");

    var t1 = $("#t1");
    var t2 = $("#t2");
    var t3 = $("#t3");
    var update = $("#update");
    var alv1 = $("#alv1");
    var alv2 = $("#alv2");
    var alv3 = $("#alv3");

    var at1 = $("#at1");
    var at2 = $("#at2");
    var at3 = $("#at3");
    var add = $("#add");
    lv2.bind("blur",function () {
        if(lv2.val()==0){
            validateTip(t1,{"color":"red"},imgNo + " * 请选择一级分类",false);
        }else{
            validateTip(t1,{"color":"red"},imgYes ,true);
        }
    });

    lv3.bind("blur",function () {
       if(lv3.val()==0){
           validateTip(t2,{"color":"red"},imgNo + " * 请选择二级分类",false);
       }else {
           validateTip(t2,{"color":"red"},imgYes ,true);
       }
    });

    $("#typeName").bind("blur",function () {
       if($("#typeName").val()==null ||$("#typeName").val()==""){
           validateTip(t3,{"color":"red"},imgNo + " * 分类名不能为空",false);
       }else {
           validateTip(t3,{"color":"red"},imgYes,true);
       }
    });

    alv2.bind("blur",function () {
        if(alv2.val()==0){
            validateTip(at1,{"color":"red"},imgNo + " * 请选择一级分类",false);
        }else{
            validateTip(at1,{"color":"red"},imgYes ,true);
        }
    });

    alv3.bind("blur",function () {
        if(alv3.val()==0){
            validateTip(at2,{"color":"red"},imgNo + " * 请选择二级分类",false);
        }else {
            validateTip(at2,{"color":"red"},imgYes ,true);
        }
    });

    $("#atypeName").bind("blur",function () {
        if($("#atypeName").val()==null ||$("#atypeName").val()==""){
            validateTip(at3,{"color":"red"},imgNo + " * 分类名不能为空",false);
        }else {
            validateTip(at3,{"color":"red"},imgYes,true);
        }
    });

    up.bind("click",function () {
        pageNo = Number(currentNo.val())-1;
        if(pageNo>1){
            currentNo.val(pageNo);
            sub(pageNo)
        }else {
            currentNo.val(1);
            sub(1);
        }
    });

    down.bind("click",function () {
        pageNo = Number(currentNo.val())+1;
        if(pageNo<last.attr("name")){
            currentNo.val(pageNo);
            sub(pageNo);
        } else{
            currentNo.val(last.attr("name"));
            sub(last.attr("name"));
        }
    });

    last.bind("click",function () {
        var vlast = last.attr("name");
        sub(vlast);
    });

    first.bind("click",function () {
        currentNo.val(1);
        sub(p1);
    });

    second.bind("click",function () {
        currentNo.val(p2);
        sub(p2);
    });

    third.bind("click",function () {
        currentNo.val(p3);
        sub(p3);
    });
    function type(t){
        if(t=="1"){
            return "一级分类";
        }else if(t=="2"){
            return "二级分类";
        }else {
            return "三级分类";
        }
    }
    function parent(p){
        if(p==null){
            return "无";
        }else {
            return p;
        }
    }

    $.ajax({
        url : "easybuy/product/typeList",
        type : "GET",
        data :  {"pageIndex":pageNo},
        dataType : "json",
        success : function (data) {
            last.attr("name",data.pageCount);
            var th = "<table class='mem_tab' style=\"width:1400px;  text-align:center; margin-top:20px;\" cellspacing=\"0\" cellpadding=\"0\">";
            th +="<tr style='font-size: 20px;height: 100px'><td style='width: 240px'>选择</td>";
            th +="<td width='240'>分类名称</td>";
            th +="<td width='240'>分类级别</td>";
            th +="<td width='240'>父级分类</td>";
            th +="<td>操作</td></tr>";
            form.append(th);
            for(var i=0;i<data.list.length;i++){
                form.append("<tr><td><input type='radio'  style='zoom: 150%;' name='xx' onclick='showType("+data.list[i].id+")'></td>"+
                    "<td id='name'>"+data.list[i].name+"</td>"+
                    "<td>"+type(data.list[i].type)+"</td>"+
                    "<td>"+parent(data.list[i].pname)+"</td>"+
                    "<td>"+"<a href='javascript:void(0)' onclick='del("+data.list[i].id+")'>删除</a>"+"</td></tr></table>");
            }
        },
        error : function () {
            alert("出现错误");
        }
    });

    function sub(pageNo){
        form.html("");
        //alert($("#pageNo").val());
        $.ajax({
            url : "easybuy/product/typeList",
            type : "GET",
            data : {"pageIndex":pageNo},
            dataType: "json",
            success : function(data){

                var th = "<table class='mem_tab' style=\"width:1400px; text-align:center; margin-top:20px;\" cellspacing=\"0\" cellpadding=\"0\">";
                th +="<tr style='font-size: 20px;height: 100px'><td style='width: 240px'>选择</td>";
                th +="<td width='240'>分类名称</td>";
                th +="<td width='240'>分类级别</td>";
                th +="<td width='240'>父级分类</td>";
                th +="<td >操作</td></tr>";
                form.append(th);
                for(var i=0;i<data.list.length;i++){
                    form.append("<tr><td><input type='radio' style='zoom: 150%;' name='xx' onclick='showType("+data.list[i].id+")'></td>"+
                        "<td id='name'>"+data.list[i].name+"</td>"+
                        "<td>"+type(data.list[i].type)+"</td>"+
                        "<td>"+parent(data.list[i].pname)+"</td>"+
                        "<td>"+"<a href='javascript:void(0)' onclick='del("+data.list[i].id+")'>删除</a>"+"</td></tr></table>");
                }
            },
            error : function (XMLHttpRequest, textStatus) {
                alert("出现错误");
                console.log(XMLHttpRequest.status);
                console.log(XMLHttpRequest.readyState);
                console.log(textStatus);
            }
        });
    }
    update.bind("click",function () {
        if(t1.attr("validateStatus") != "true"){
            lv2.blur();
        }else if(t2.attr("validateStatus") != "true"){
            lv3.blur();
        }else if(t3.attr("validateStatus") != "true"){
            $("#typeName").blur();
        }else{
            if(confirm("确认修改")){
                $.ajax({
                    url : "easybuy/product/updateType",
                    type : "post",
                    dataType : "json",
                    data : $("#TypeForm").serialize(),
                    success : function(data){
                        if(data>0){
                            alert("修改成功");
                            location.reload();
                        }else {
                            alert("提交数据错误");
                        }

                    },
                    error : function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("出现错误");
                        console.log(XMLHttpRequest.status);
                        console.log(XMLHttpRequest.readyState);
                        console.log(textStatus);
                        //alert(XMLHttpRequest.status);
                        //alert(XMLHttpRequest.readyState);
                        //alert(textStatus);
                    }

                });
            }
        }
    });

    alv1.bind("click",function () {
       if(alv1.val()=="1"){
           $("#ap1").hide();
           $("#ap2").hide();
           $("#alv2").attr("disabled",true);
           $("#alv3 option[value='"+0+"']").attr("selected","selected");
       }else if(alv1.val()=="2"){
           $("#ap1").show();
           $("#ap2").hide();
           One();
       }else if(alv1.val()=="3"){
           $("#ap1").hide();
           $("#alv2").attr("disabled",true);
           $("#ap2").show();
           Two();
           //fillL2();
           //fillL3();
       }

    });

    function One(){
        $.ajax({
            async : false,
            url : "easybuy/product/TypeOne",
            type : "post",
            dataType: "json",
            success : function (data) {
                var str = "";
                alv2.html("");
                for(var i=0 ; i<data.length; i++){
                    str += "<option value='"+data[i].id+"'>"+data[i].name+"</option>"
                }
                alv2.append(str);
            }
        });
    }
    function Two(){

        $.ajax({
            async : false,
            url : "easybuy/product/TypeTwo",
            type : "post",
            dataType: "json",
            success : function (data) {
                var str = "";
                alv3.html("");
                for(var i=0 ; i<data.length; i++){
                    str += "<option value='"+data[i].id+"'>"+data[i].name+"</option>"
                }
                alv3.append(str);
            }
        });

    }

    /*alv1.change(function () {
        fillL2();
        fillL3();
    });*/
    /*alv2.change(function () {
        fillL3();
    });*/

    function fillL3(){
        var pcode = alv2.val();
        $.ajax({
            async : false,
            url : "easybuy/product/getType",
            data : {"id":pcode},
            type : "post",
            dataType: "json",
            success : function (data) {
                var str = "";
                alv3.html("");
                for(var i=0 ; i<data.length; i++){
                    str += "<option value='"+data[i].id+"'>"+data[i].name+"</option>"
                }
                alv3.append(str);
            }
        });

    }


});
function del(id) {
    $.ajax({
        url : "easybuy/product/delType",
        type : "get",
        data : {id : id},
        dataType : "json",
        success : function(data) {
            if (data>0) {
                alert("删除成功");
                location.reload();
            }
        },
        error : function (XMLHttpRequest, textStatus) {
            alert("出现错误");
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }
    });
}

function showType(id){
    $.ajax({
        url : "easybuy/product/viewType",
        data : {"id":id},
        dataType : "json",
        type : "get",
        success : function (data) {
            if(data !=null){
                $("#typeName").val(data.name);
                $("#TypeId").val(data.id);
                //选中一级分类
                switch(data.type){
                    //分类级别为1，一级，二级分类隐藏,且不能修改分类等级
                    case 1 : $("#lv1 option[value='1']").attr("selected","selected");
                             $("#p1").hide();
                             $("#p2").hide();
                             break;
                    //分类级别为2，二级分类隐藏
                    case 2 : $("#lv1 option[value='2']").attr("selected","selected");
                             $("#p1").show();
                             $("#lv2").attr("disabled",false);
                             $("#p2").hide();
                             $("#lv2 option[name='"+data.pname+"']").attr("selected","selected");
                             break;
                    //一级分类，分类级别无法修改
                    case 3 : $("#lv1 option[value='3']").attr("selected","selected");
                             $("#lv1").attr("disabled","disabled");
                             $("#lv2").attr("disabled","disabled");
                             $("#p1").show();
                             $("#p2").show();
                             $("#lv2 option[name='"+data.ppname+"']").attr("selected","selected");
                             $("#lv3 option[name='"+data.pname+"']").attr("selected","selected");
                             break;
                }

            }
        }

    });
}

function chooseType(){
    if(confirm("确认提交")){

        $.ajax({
            url : "easybuy/product/insert",
            dataType : "json",
            type : "post",
            data : $("#addForm").serialize(),
            success : function (data) {
                if(data>0){
                    alert("新增成功");
                    location.reload();
                }
            },
            error : function (XMLHttpRequest, textStatus) {
                alert("出现错误");
                console.log(XMLHttpRequest.status);
                console.log(XMLHttpRequest.readyState);
                console.log(textStatus);
            }
        });
    }



}